/*
 * Author: kael http://weibo.com/loskael
 */
package los.kael.image;

import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * 
 * @author kael http://weibo.com/loskael
 * @version $FileName: image.java $Date: 2011-11-4 上午9:13:11 -0400 2011
 * @since 1.0
 * 
 */
public class Images {
	/**
	 * 0:no-repeat default 1:no-repeat left 2:no-repeat right 3:repeat-x
	 * 4:repeat-y
	 */
	public static final int NO_REPEAT = 0;
	public static final int NO_REPEAT_LEFT = 1;
	public static final int NO_REPEAT_RIGHT = 2;
	public static final int REPEAT_X = 3;
	public static final int REPEAT_Y = 4;
	private String name;
	private Image image;
	private int width;
	private int height;
	private int positionX = 0;
	private int positionY = 0;
	private int repeatAndFloat = NO_REPEAT;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Image getImage() {
		return image;
	}

	public void setImage(Image image) {
		this.image = image;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getPositionX() {
		return positionX;
	}

	public void setPositionX(int positionX) {
		this.positionX = positionX;
	}

	public int getPositionY() {
		return positionY;
	}

	public void setPositionY(int positionY) {
		this.positionY = positionY;
	}

	public int getRepeatAndFloat() {
		return repeatAndFloat;
	}

	public void setRepeatAndFloat(int repeatAndFloat) {
		this.repeatAndFloat = repeatAndFloat;
	}

	public Images(String filename) throws IOException {
		this.name = filename;
		if (name.indexOf("_repeatx.") != -1) {
			this.repeatAndFloat = REPEAT_X;
		} else if (name.indexOf("_repeaty.") != -1) {
			this.repeatAndFloat = REPEAT_Y;
		} else if (name.indexOf("_left.") != -1) {
			this.repeatAndFloat = NO_REPEAT_LEFT;
		} else if (name.indexOf("_right.") != -1) {
			this.repeatAndFloat = NO_REPEAT_RIGHT;
		}
		image = ImageIO.read(new File(filename));
		this.width = image.getWidth(null);
		this.height = image.getHeight(null);
	}

	public String toString() {
		String tag = name.replace("\\", "/");
		int index = tag.indexOf("/");
		int indexHead = 0;
		if(tag.indexOf("_hover.") != -1){
			tag = tag.replace("_hover.", ":hover.");
		}
		while (index != -1) {
			tag = tag.substring(index + 1);
			index = tag.indexOf("/");
		}
		index = tag.indexOf(".");
		indexHead = tag.indexOf("_");
		indexHead = indexHead == -1 ? 0 : indexHead + 1;
		tag = "." + tag.substring(indexHead, index) + "{";
		String repeat = "no-repeat";
		if (repeatAndFloat == REPEAT_X) {
			repeat = "repeat-x";
			tag = tag.replace("_repeatx", "");
		} else if (repeatAndFloat == REPEAT_Y) {
			repeat = "repeat-y";
			tag = tag.replace("_repeaty", "");
		}
		return tag + "width:" + width + "px;height:" + height + "px;background-position:" + (0 - positionX)
				+ ((positionX == 0) ? " " : "px ") + (0 - positionY) + ((positionY == 0) ? " " : "px")
				+ ((repeat.equals("no-repeat")) ? "" : ";background-repeat:" + repeat) + "}\n";
	}

}
